<?php

defined( 'IN_CLASS' ) or die( 'Access Error!' );

class ModelShare extends base {
	
	//定义类型与图标对应数组,加icon_前缀即为图标文件名,默认或说未知类型用icon_.gif
	var $icons = array( 'html' => 'htm' , 'htm' => 'htm' , 'mht' => 'htm' , 'rar' => 'rar' , 'zip' => 'rar' , 
				'tar' => 'rar' ,'cab' => 'rar' , 'gz' => 'rar' , 'ace' => 'rar' , 'jar' => 'rar' ,
				'doc' => 'office' , 'xls' => 'office' , 'ppt' => 'office' , 'pps' => 'office' , 'pot' => 'office', 
				'mdb' => 'office' , 'mda' => 'office' ,	'txt' => 'txt' , 'log' => 'txt' , 'ini' => 'txt' , 
				'css' => 'txt' , 'php' => 'script' , 'asp' => 'script' , 'aspx' => 'script' , 'jsp' => 'script' , 
				'cfm' => 'script' , 'php3' => 'script' , 'cfml' => 'script' , 'php4' => 'script' , 'chm' => 'chm' , 
				'hlp' => 'chm' , 'pdf' => 'pdf' , 'jpeg' => 'image' , 'jpg' => 'image' , 'gif' => 'image' , 
				'bmp' => 'image' , 'tif' => 'image' , 'tiff' => 'image' , 'png' => 'image' , 'wmf' => 'image' , 
				'psd' => 'image' , 'asf' => 'video' , 'wmv' => 'video' , 'avi' => 'video' , 'rm' => 'video' , 
				'rmvb' => 'video' , 'mov' => 'video' , 'swf' => 'video' , 'mpg' => 'video' , 'wma' => 'audio' , 
				'mp3' => 'audio' , 'mid' => 'audio' , 'midi' => 'audio' , 'ra' => 'audio' , 'wav' => 'audio' ,
				'exe' => 'exe' );
	
	public function getNew() {
		$sql = "select t1.*,t2.name as owner_name 
				from $this->tshare as t1,$this->tmember as t2 where t1.owner=t2.id order by t1.id desc limit 1";
		$aShare = model::dbuser()->get_one( $sql );
		return $aShare;
	}
	
	public function get( $fid ) {
		$sql = "SELECT * FROM $this->tshare WHERE id='{$fid}'";
		$re = model::dbuser()->get_one( $sql );
		return is_array( $re ) ? $re : array();
	}
	
	public function getpart( $limit ) {
		$sql = "SELECT SQL_CALC_FOUND_ROWS t1.*, t1.size/1024 ksize, t2.name FROM $this->tshare t1, $this->tmember t2  
				WHERE t1.owner=t2.id ORDER BY t1.id DESC {$limit} ";
		$shs = model::dbuser()->get_all( $sql );
		return is_array( $shs ) ? $shs : array();
	}
	
	public function getused() {
		$sql = "SELECT SUM(size) total FROM $this->tshare ";
		$aUsed = model::dbuser()->get_one( $sql );
		return (int)$aUsed['total'];
	}
	
	/**
	 * 上传文件方法
	 *
	 * @param string $uid
	 * @param string $brief
	 * @return int
	 */
	public function up( $uid, $brief ) {
		$new_file_name = time(); //新文件名
		$new_file = DIR_DAT . "share/$new_file_name.dat";
		!is_dir( DIR_DAT . 'share/' ) && mkdir( DIR_DAT . 'share/', 0777, true );
		
		if ( move_uploaded_file( $_FILES['file']['tmp_name'] , $new_file ) ) {
			$old_file_ext = '';
			$old_file_name = $_FILES['file']['name'];
			$tmp_piece = explode( '.' , $_FILES['file']['name'] );
			$tmp_len = count( $tmp_piece );
			if ( $tmp_len > 1 ) {
				if ( preg_match( '/^\w{1,4}$/' , $tmp_piece[ $tmp_len - 1 ] ) > 0) {
					$old_file_ext = strtolower( $tmp_piece[ $tmp_len - 1 ] );
					array_pop( $tmp_piece );  //array_pop最后一个元素出栈
					$old_file_name = implode ( '.', $tmp_piece );
				}
			}
			if ( strlen( $old_file_name ) > 32 ) $old_file_name = func::c_substr( $old_file_name, 15 ) . '~~';
			$file_icon = $this->icons[ $old_file_ext ];
			$sql = "INSERT INTO $this->tshare SET filename='{$new_file_name}',owner='{$uid}',
					oldname='{$old_file_name}',ext='{$old_file_ext}',size={$_FILES['file']['size']},
					icon='{$file_icon}',mime='{$_FILES['file']['type']}',time=NOW(),brief='{$brief}'";
			model::dbuser()->exe( $sql );
			return model::dbuser()->update_rows();
		}
		
		return 0;
	}
	
	public function del( $fid, $filename ) {
		$sql = "DELETE FROM $this->tshare WHERE id='{$fid}' LIMIT 1";
		$del = unlink( DIR_DAT . "share/$filename.dat" );
		$del ? model::dbuser()->exe( $sql ) : '';
		return (int)$del;
	}
}